/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_overlap_sample.h
 * Desc:    Mesh overlap sample shader
 * Version: 1.02
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		texture2DAlphaFadeDiscard(s_texture_10,s_texcoord_9.x);
	#endif
	
	float2 overlap_texcoord = s_texcoord_6.xy * overlap_transform.xy + overlap_transform.zw;
	
	half4 base_diffuse = texture(s_texture_1,s_texcoord_0.xy) * base_diffuse_color;
	half4 overlap_diffuse = texture(s_texture_4,overlap_texcoord) * overlap_diffuse_color;
	
	half mask = texture(s_texture_0,s_texcoord_6.xy * mask_transform.xy + mask_transform.zw).x;
	half angle = saturate((s_texcoord_6.w - angle_value + angle_threshold * 0.5f) / max(angle_threshold,1e-6f));
	half overlap = saturate((mask * angle * overlap_diffuse.w - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	half4 diffuse = lerp(base_diffuse,overlap_diffuse,overlap);
	
	#ifdef ALPHA_TEST
		#ifdef MULTISAMPLE_0 || USE_ARB_SAMPLE_SHADING
			if(diffuse.w <= 0.5f) discard;
		#else
			diffuse.w = diffuse.w * 4.0f - 1.5f;
			if(diffuse.w <= 0.2f) discard;
		#endif
	#endif
	
	half3 overlap_normal;
	half3 base_normal = texture2DNormal(s_texture_2,s_texcoord_0.xy);
	#ifdef SUBSTITUTE
		overlap_normal = texture2DNormal(s_texture_5,overlap_texcoord);
	#else
		overlap_normal.xy = base_normal.xy + texture2DNormal(s_texture_5,overlap_texcoord).xy;
		overlap_normal.z = sqrt(saturate(1.0f - dot(overlap_normal.xy,overlap_normal.xy)));
	#endif
	
	half4 base_specular = texture(s_texture_3,s_texcoord_0.xy) * base_specular_color;
	half4 overlap_specular = texture(s_texture_6,overlap_texcoord) * overlap_specular_color;
	
	half3 normal = normalize(lerp(base_normal,overlap_normal,overlap));
	half4 specular = lerp(base_specular,overlap_specular,overlap);
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		texture2DAlphaFadeDiscard(s_texture_10,s_sampler_10,IN.position.xy,IN.texcoord_9.x);
	#endif
	
	float2 overlap_texcoord = IN.texcoord_6.xy * overlap_transform.xy + overlap_transform.zw;
	
	half4 base_diffuse = s_texture_1.Sample(s_sampler_1,IN.texcoord_0.xy) * base_diffuse_color;
	half4 overlap_diffuse = s_texture_4.Sample(s_sampler_4,overlap_texcoord) * overlap_diffuse_color;
	
	half mask = s_texture_0.Sample(s_sampler_0,IN.texcoord_6.xy * mask_transform.xy + mask_transform.zw).x;
	half angle = saturate((IN.texcoord_6.w - angle_value + angle_threshold * 0.5f) / max(angle_threshold,1e-6f));
	half overlap = saturate((mask * angle * overlap_diffuse.w - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	half4 diffuse = lerp(base_diffuse,overlap_diffuse,overlap);
	
	#ifdef ALPHA_TEST
		#ifdef MULTISAMPLE_0 || USE_ALPHA_TEST_LEVEL_10_1
			if(diffuse.w <= 0.5f) discard;
		#else
			diffuse.w = diffuse.w * 4.0f - 1.5f;
			if(diffuse.w <= 0.2f) discard;
		#endif
	#endif
	
	half3 overlap_normal;
	half3 base_normal = texture2DNormal(s_texture_2,s_sampler_2,IN.texcoord_0.xy);
	#ifdef SUBSTITUTE
		overlap_normal = texture2DNormal(s_texture_5,s_sampler_5,overlap_texcoord);
	#else
		overlap_normal.xy = base_normal.xy + s_texture_5.Sample(s_sampler_5,overlap_texcoord).xy;
		overlap_normal.z = sqrt(saturate(1.0f - dot(overlap_normal.xy,overlap_normal.xy)));
	#endif
	
	half4 base_specular = s_texture_3.Sample(s_sampler_3,IN.texcoord_0.xy) * base_specular_color;
	half4 overlap_specular = s_texture_6.Sample(s_sampler_6,overlap_texcoord) * overlap_specular_color;
	
	half3 normal = normalize(lerp(base_normal,overlap_normal,overlap));
	half4 specular = lerp(base_specular,overlap_specular,overlap);
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		tex2DAlphaFadeDiscard(s_texture_10,IN.texcoord_7,IN.texcoord_7.z);
	#endif
	
	float2 overlap_texcoord = IN.texcoord_6.xy * overlap_transform.xy + overlap_transform.zw;
	
	half4 base_diffuse = tex2D(s_texture_1,IN.texcoord_0.xy) * base_diffuse_color;
	half4 overlap_diffuse = tex2D(s_texture_4,overlap_texcoord) * overlap_diffuse_color;
	
	half mask = tex2D(s_texture_0,IN.texcoord_6.xy * mask_transform.xy + mask_transform.zw).x;
	half angle = saturate((IN.texcoord_6.w - angle_value + angle_threshold * 0.5f) / max(angle_threshold,1e-6f));
	half overlap = saturate((mask * angle * overlap_diffuse.w - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	half4 diffuse = lerp(base_diffuse,overlap_diffuse,overlap);
	
	#ifdef ALPHA_TEST
		#ifdef MULTISAMPLE_0 || !USE_ALPHA_TO_COVERAGE
			if(diffuse.w <= 0.5f) discard;
		#else
			diffuse.w = diffuse.w * 4.0f - 1.5f;
		#endif
	#endif
	
	half3 overlap_normal;
	half3 base_normal = tex2DNormal(s_texture_2,IN.texcoord_0.xy);
	#ifdef SUBSTITUTE
		overlap_normal = tex2DNormal(s_texture_5,overlap_texcoord);
	#else
		overlap_normal.xy = base_normal.xy + tex2DNormal(s_texture_5,overlap_texcoord).xy;
		overlap_normal.z = sqrt(saturate(1.0f - dot(overlap_normal.xy,overlap_normal.xy)));
	#endif
	
	half4 base_specular = tex2D(s_texture_3,IN.texcoord_0.xy) * base_specular_color;
	half4 overlap_specular = tex2D(s_texture_6,overlap_texcoord) * overlap_specular_color;
	
	half3 normal = normalize(lerp(base_normal,overlap_normal,overlap));
	half4 specular = lerp(base_specular,overlap_specular,overlap);
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifdef USE_ALPHA_FADE && ALPHA_FADE
		tex2DAlphaFadeDiscard(s_texture_10,IN.texcoord_9,IN.texcoord_9.z);
	#endif
	
	float2 overlap_texcoord = IN.texcoord_8.xy * overlap_transform.xy + overlap_transform.zw;
	
	half4 base_diffuse = h4tex2D(s_texture_1,IN.texcoord_0.xy) * base_diffuse_color;
	half4 overlap_diffuse = h4tex2D(s_texture_4,overlap_texcoord) * overlap_diffuse_color;
	
	half mask = h1tex2D(s_texture_0,IN.texcoord_8.xy * mask_transform.xy + mask_transform.zw);
	half angle = saturate((IN.texcoord_8.w - angle_value + angle_threshold * 0.5f) / max(angle_threshold,1e-6f));
	half overlap = saturate((mask * angle * overlap_diffuse.w - mask_value + mask_threshold * 0.5f) / max(mask_threshold,1e-6f));
	
	half4 diffuse = lerp(base_diffuse,overlap_diffuse,overlap);
	
	#ifdef ALPHA_TEST
		if(diffuse.w <= 0.5f) discard;
	#endif
	
	half3 overlap_normal;
	half3 base_normal = tex2DNormal(s_texture_2,IN.texcoord_0.xy);
	#ifdef SUBSTITUTE
		overlap_normal = tex2DNormal(s_texture_5,overlap_texcoord);
	#else
		overlap_normal.xy = base_normal.xy + h2tex2D(s_texture_5,overlap_texcoord);
		overlap_normal.z = sqrt(saturate(1.0f - dot(overlap_normal.xy,overlap_normal.xy)));
	#endif
	
	half4 base_specular = h4tex2D(s_texture_3,IN.texcoord_0.xy) * base_specular_color;
	half4 overlap_specular = h4tex2D(s_texture_6,overlap_texcoord) * overlap_specular_color;
	
	half3 normal = normalize(lerp(base_normal,overlap_normal,overlap));
	half4 specular = lerp(base_specular,overlap_specular,overlap);
	
#endif
